/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import wso.dto.WsoProduct;
import wso.global.BaseDao;

/**
 *
 * @author Violet_Ink
 */
public class ProductDaoImpl extends BaseDao implements ProductDao {

    public void saveOrUpdateProduct(WsoProduct product) {
        saveOrUpdate(product);
    }

    public void deleteProduct(String productid) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("update WsoProduct pro set pro.isdeleted = true where pro.productid = :productid");
        query = session.createQuery(str.toString());
        query.setString("productid", productid);

        query.executeUpdate();
    }

    public WsoProduct[] getAllProduct() {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("from WsoProduct pro where pro.isdeleted = false");
        query = session.createQuery(str.toString());
        List productList = query.list();
        WsoProduct[] result = new WsoProduct[productList.size()];
        productList.toArray(result);

        return result;
    }

    public WsoProduct getProduct(String productid) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();

        str.append("from WsoProduct pro where pro.productid = :productid and pro.isdeleted = false");
        query = session.createQuery(str.toString());
        query.setString("productid", productid);

        return (WsoProduct) query.uniqueResult();
    }

}
